package com.tr.rentcar.controller.houtai.car;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.model.bean.CarAttr;
import com.tr.rentcar.model.dao.CarAttrDao;

@WebServlet("/carattrval")
public class CarattrvalControllor extends HttpServlet {
	CarAttr carattr = new CarAttr();
	CarAttrDao carattrdao = new CarAttrDao();
	private static final long serialVersionUID = 1L;
	Integer csId;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String id = req.getParameter("id");
			System.out.println(id);
			csId = Integer.parseInt(id);
			req.setAttribute("csid", csId);
			List<CarAttr> carattrval = carattrdao.getAllById(csId);
			req.setAttribute("carattrval", carattrval);
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getRequestDispatcher("/WEB-INF/jsp/houtai_carattrval.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			req.setCharacterEncoding("utf-8");
			resp.setContentType("application/json;charset=utf-8");
			System.out.println(csId);
			List<CarAttr> carattrval = carattrdao.getAllById(csId);
			PrintWriter out = resp.getWriter();
			out.print(JSONArray.toJSONString(carattrval));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
